import asyncio
import logging
from fileinput import filename
from typing import Tuple, Union, List, Optional, Callable

from fastapi import FastAPI

try:
    from afast_core.core_libs.cashews import Cache
except Exception:
    pass
from afast_core.core_plugins.pluginbase import IBasePlugin as BasePlugin
from pydantic import BaseSettings as Settings



global_cashews_cache: Union[Callable,Cache] = lambda: None
global_cashews : Union[Callable,Cache] = lambda: None

class AsyncCashewsPluginClient(BasePlugin):
    # 设置插件默认的参数信息
    name = 'AsyncCashewsCache'

    class CacheSettings(Settings):
        url: str = "mem://"

    def setup(self, app: FastAPI, name: str = None, *args, **kwargs):
        """插件初始化"""
        global global_cashews_cache
        global global_cashews
        self.app = app
        self._cache = Cache()
        self._cache.setup(self.settings.url)
        app.async_cache = self._cache
        global_cashews_cache = self._cache
        global_cashews = self._cache